package com.easy;

/**
 * Created by Lxk on 2020/1/28.
 */
public class Solution606 {

    private void treeToStr(TreeNode node, StringBuffer sb) {
        if (node == null) {
            return;
        }
        sb.append(node.val);
        if (node.left != null) {
            sb.append("(");
            treeToStr(node.left, sb);
            sb.append(")");
        }
        if (node.right != null) {
            if (node.left == null) {
                sb.append("()");
            }
            sb.append("(");
            treeToStr(node.right, sb);
            sb.append(")");
        }
    }

    public String tree2str(TreeNode t) {
        StringBuffer sb = new StringBuffer();
        treeToStr(t, sb);
        return sb.toString();
    }

}
